package com.baizhi.xxn.service;

import com.baizhi.xxn.dao.UserMapper;
import com.baizhi.xxn.entity.User;
import com.baizhi.xxn.entity.UserExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String,Object> queryAllPage(int page, int pageSize) {
        //调用
        User user=new User();
        Map<String,Object> map=new HashMap<>();
        RowBounds rowBounds=new RowBounds((page-1)*pageSize,pageSize);
        List<User> users = userMapper.selectByRowBounds(user,rowBounds);
        int totalCount = userMapper.selectCount(null);
        map.put("total",totalCount);
        map.put("page",page);
        map.put("rows",users);
        return map;
    }
    //修改状态
    @Override
    public Map<String, String> update(User user) {
        /*User users=new User();
        Map<String,String> map=new HashMap<>();
        users.setId(user.getId());
        users.setStatus(user.getStatus());
        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(user.getId());
        userMapper.updateByExampleSelective(user,userExample);*/
        Map<String,String> map=new HashMap<>();
        int i = userMapper.updateByPrimaryKeySelective(user);
        try{
            map.put("message", "操作成功");
            map.put("status", "200");
            return map;
        } catch (Exception e){
            map.put("message", "操作失败");
            map.put("status", "400");
            return map;
        }
    }


}
